/**
 *
 * Created by Song, YeWen, 1/19/2012 4:35:20 PM
 *
 */

var XBK_FLV = "xbk_flv";
var CCTV    = "cctv";
var XBK_MP4 = "xbk_mp4";
var XIYOU   = "xiyou";

function get3ByteNumberString(param1)
{
    if (param1 < 10)
    {
        return "00" + param1;
    }// end if
    if (param1 < 100)
    {
        return "0" + param1;
    }// end if
    if (param1 < 1000)
    {
        return "" + param1;
    }// end if
    return "000";
}// end function

function xiyou_parser(param1, param2)
{
    switch(param2) {
    case "true":
        param2 = CCTV;
        break;
    case "xbk_flv":
        param2 = XBK_FLV;
        break;
    case "xbk_mp4":
        param2 = XBK_MP4;
        break;
    default:
        param2 = XIYOU;
        break;
    }// end switch
    var _loc_8;
    var _loc_9;
    var _loc_10;
    var _loc_3;
    var _loc_4 = param1.split("#");
    if (_loc_4.length != 2)
    {
        return param1;
    }// end if
    var _loc_5 = _loc_4[0];
    var _loc_6 = String(_loc_4[1]).split("_");
    if (String(_loc_4[1]).split("_").length == 1 && param2 != XIYOU)
    {
        if (param2 == CCTV || param2 == XBK_MP4)
        {
            _loc_3 = _loc_5 + ".mp4";
        }
        else if (param2 == XBK_FLV)
        {
            _loc_3 = _loc_5 + ".flv";
        }// end else if
        return _loc_3;
    }// end if
    var _loc_7 = 0;
    while (_loc_7 < _loc_6.length)
    {
        if (param2 == CCTV)
        {
            _loc_3 = _loc_5 + "-" + (_loc_7 + 1) + ".mp4";
        }
        else
        {
            _loc_3 = _loc_5 + "_" + get3ByteNumberString(_loc_7 + 1) + ".mp4";
        }// end else if
        _loc_7++;
    }// end while
    return _loc_3;
}// end function

function validateVideoUrl(ori_url)
{
    var duration = 0;
    var html = null;
    var dsp_cmd = null;
    var vid = null;
    var json_str = null;
    var json = null;
    var sb = [];
    var data;
    var site_name = copyBetween(ori_url, 'http://', '/v-');
    if (site_name == "xiyou.cntv.cn") {
        vid = copyBetween(ori_url, 'v-', '.html');
        dsp_cmd = "http://xiyou.cntv.cn/interface/index?videoId=" + vid;
        json_str = download_page(dsp_cmd);
        json = eval('(' + json_str + ')');
        data = json.data;
        for (var i=0; i<data[0].videoList.length; i++) {
            sb.push(xiyou_parser(data[0].videoList[i].videoFilePath, data[0].isCCTV));
            duration += parseInt(data[0].timeSpan);
        }
    } else {
        html = download_page(ori_url);
        // var initMyAray=  '13145e9cfebc4301a812b5758d0378ec';
        vid = html.match(/initMyAray\s*=\s*'(\w+)'/);
        if (!vid) {
            // ("videoCenterId","1d42e35f25b2467db261e70e90b759b4");
            vid = html.match(/videoCenterId",\s*"(\w+)"/);
            if (!vid)
                return 'NotFoundVideo';
        }
        dsp_cmd = "http://vdn.apps.cntv.cn/api/getHttpVideoInfo.do?pid=" + vid[1];
        json_str = download_page(dsp_cmd);
        json = eval('(' + json_str + ')');
        data = json.video;
        for(var i=0;i<data.chapters.length;i++){
            sb.push(data.chapters[i].url);
            duration += parseInt(data.chapters[i].duration);
        }
    }
    if(sb.length>0){
        html = download_page(ori_url);
        //var matches = html.match(/<title>(.*?)<\/title>/);
        var matches = html.match(/<title>([\s\S]*?)<\/title>/);
        if(matches){
           return 'success_title:' + encodeURIComponent(matches[1])
                  + '\n video_urls:' + encodeURIComponent(sb)
                  + '\n seconds:' + Math.round(parseInt(duration));
        }
    }
    return 'error:empty_path';
}

// case-1: http://xiyou.cntv.cn/v-5ae80be2-dd8d-11df-9117-001e0bbb2442.html
// case-2: http://xiyou.cntv.cn/v-c26ece9a-a52f-11df-bdae-001e4f1f5c05.html
// case-3: http://sports.cntv.cn/20120119/106704.shtml
// case-4: http://ncpa-classic.cntv.cn/2011/12/24/VIDE1324701005721541.shtml
function js_get_media_url(ori_url)
{
    var html = null;
    var dsp_cmd = null;
    var vid = null;
    var json_str = null;
    var json = null;
    var sb = [];
    var data;
    var site_name = copyBetween(ori_url, 'http://', '/v-');
    if (site_name == "xiyou.cntv.cn") {
        vid = copyBetween(ori_url, 'v-', '.html');
        dsp_cmd = "http://xiyou.cntv.cn/interface/index?videoId=" + vid;
        json_str = download_page(dsp_cmd);
        json = eval('(' + json_str + ')');
        data = json.data;
        for (var i=0; i<data[0].videoList.length; i++) {
            sb.push(xiyou_parser(data[0].videoList[i].videoFilePath, data[0].isCCTV));
        }
        return sb;
    } else {
        html = download_page(ori_url);
        // var initMyAray=  '13145e9cfebc4301a812b5758d0378ec';
        vid = html.match(/initMyAray\s*=\s*'(\w+)'/);
        if (!vid) {
            // ("videoCenterId","1d42e35f25b2467db261e70e90b759b4");
            vid = html.match(/videoCenterId",\s*"(\w+)"/);
            if (!vid)
                return '';
        }
        dsp_cmd = "http://vdn.apps.cntv.cn/api/getHttpVideoInfo.do?pid=" + vid[1];
        json_str = download_page(dsp_cmd);
        json = eval('(' + json_str + ')');
        data = json.video;
        for(var i=0;i<data.chapters.length;i++){
            sb.push(data.chapters[i].url);
        }
        alert(sb);
        return sb;
    }
    return '';
}
